package utils

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"github.com/wonderivan/logger"
)

var OSSClient *oss.Client
var OSSBucket *oss.Bucket

func OSSInit(config *OssConfig) {
	endPoint := config.EndPoint
	accessKeyId := config.AccessKeyId
	accessKeySecret := config.AccessKeySecret
	bucketName := config.BucketName
	client, err := oss.New(endPoint, accessKeyId, accessKeySecret)
	if err != nil {
		logger.Error(err.Error())
	}
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		logger.Error(err.Error())
	}
	// 列举文件做一下测试
	marker := ""
	for {
		lsResp, err := bucket.ListObjects(oss.Marker(marker))
		if err != nil {
			logger.Error(err.Error())
		}
		for _, object := range lsResp.Objects {
			fmt.Println("Bucket: ", object.Key)
		}
		if lsResp.IsTruncated {
			marker = lsResp.NextMarker
		} else {
			break
		}
	}
	logger.Info("OSS初始化完成")
	OSSBucket = bucket
	OSSClient = client

}
